#include "lrcpage.h"
#include "ui_lrcpage.h"
#include<QPushButton>
#include<QFile>
#include<QDebug>
lrcpage::lrcpage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::lrcpage)
{
    ui->setupUi(this);
    //设置窗口无标题栏
    setWindowFlag(Qt::FramelessWindowHint);


    ui->hideBtn->setIcon(QIcon(":/xiala11.png"));
    lrcpageAnimation=new QPropertyAnimation(this,"geometry",this);
    lrcpageAnimation->setDuration(400);
    lrcpageAnimation->setEndValue(QRect(this->x(),this->height(),this->width(),this->height()));
    lrcpageAnimation->setStartValue(QRect(this->x(),0,this->width(),this->height()));
    //点击下拉框，播放动画
    connect(ui->hideBtn,&QPushButton::clicked,this,[=](){
        lrcpageAnimation->start();
    });
    //动画结束之后，窗口需要隐藏
    connect(lrcpageAnimation,&QPropertyAnimation::finished,this,[=](){
       this->hide();
    });
    showLrcWordLine(-1);
}

lrcpage::~lrcpage()
{
    delete ui;
}

bool lrcpage::praseFilePath(const QString &LrcFilePath,const QString LrcMusicName,const QString LrcMusicSinger)
{
    QFile file(LrcFilePath);
    if(!file.open(QIODevice::ReadOnly))
    {
        //打开失败
        return false;
    }
    //将上一次lrc歌词清空
    LrcWordLines.clear();
    setLrcTitle(LrcMusicName,LrcMusicSinger);
    //一次性读取一行歌词
    while(!file.atEnd())
    {
        QString LrcLineWord=file.readLine(1024);
        //解析歌词
        //[0:33.600.00]比2002年来的更晚一些
        int start=0,end=0;
        end=LrcLineWord.indexOf("]",start);
        QString LrcTime=LrcLineWord.mid(start,end-start+1);
        QString LrcWord=LrcLineWord.mid(end+1,LrcLineWord.size()-end-1-1-1);//减去\r\n
        //[分:秒.毫秒] [0:33.600.00]
        //解析分
        qint64 lineTime=0;
        start=1;
        end=LrcLineWord.indexOf(":",start);
        lineTime+=LrcLineWord.mid(start,end-start).toUInt()*60*1000;
        //解析秒
        start=end+1;
        end=LrcLineWord.indexOf(".",start);
        lineTime+=LrcLineWord.mid(start,end-start).toUInt()*1000;
        //解析毫秒
        start=end+1;
        end=LrcLineWord.indexOf(".",start);
        lineTime+=LrcLineWord.mid(start,end-start).toUInt();
        //依次将行保存
        LrcWordLines.push_back(LrcWordLine(lineTime,LrcWord));
    }

    for(auto e :LrcWordLines)
    {
        qDebug()<<e.LrcTime<<e.LrcText;
    }
    return true;
}

void lrcpage::showLrcWordLine(qint64 time)
{
    int index=getLrcWordLineIndex(time);
    if(index==-1)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineContent->setText("当前歌曲暂无歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(getLrcWord(index-3));
        ui->line2->setText(getLrcWord(index-2));
        ui->line3->setText(getLrcWord(index-1));
        ui->lineContent->setText(getLrcWord(index));
        ui->line4->setText(getLrcWord(index+1));
        ui->line5->setText(getLrcWord(index+2));
        ui->line6->setText(getLrcWord(index+3));
    }
}

int lrcpage::getLrcWordLineIndex(qint64 time)
{
    if(LrcWordLines.isEmpty())
    {
        return -1;
    }
    if(time<=LrcWordLines[0].LrcTime)
    {
        return 0;
    }
    for(int i=1;i<LrcWordLines.size();++i)
    {
        if(time>=LrcWordLines[i-1].LrcTime&&time<LrcWordLines[i].LrcTime)
        {
            //说明第i-1行还没有播放完
            return i-1;
        }
    }
    return LrcWordLines.size()-1;
}

QString lrcpage::getLrcWord(int index)
{
    if(index<0||index>=LrcWordLines.size())
    {
        return "";
    }
    return LrcWordLines[index].LrcText;
}

void lrcpage::setLrcTitle(const QString musicName, const QString musicSinger)
{
    ui->musicName->setText(musicName);
    ui->musicSinger->setText(musicSinger);
}

